﻿package com.codingbros.fireworks.particles 
{
	import flash.display.Shape;
	
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author ...
	 */
	public class DrawLineParticles  extends Shape implements ILineParticle
	{
		
		private static const MAXSPEED:Number=8;
		private static const MINSPEED:Number=4;
		
		private var currentPos:Point;
		private var lastPos:Point;
		
		private var velocity:Point;
		private var acceleration:Point;
		private var gravity:Point;	
		
		private var sideWind:Point;
		
		private var color:uint;
		private var lineThickness:Number = 0;
		
		public function DrawLineParticles()
		{			
			//currentPos=new Point();
			//fuck motion:
			currentPos=new Point(-5 + Math.random()*10, -5 + Math.random()*10);
			//currentPos=new Point(-3 + Math.random()*6, -4 + Math.random()*8);
			
			lastPos=new Point();
			
			//var angle:Number=Math.random()*360;
			var radians:Number=Math.random()*Math.PI*2;
			var speed:Number = MINSPEED + Math.random() * (MAXSPEED - MINSPEED);
			
			velocity = new Point();
			velocity.x = Math.cos(radians)*speed;
			velocity.y = Math.sin(radians)*speed;
			
			acceleration = new Point();
			gravity = new Point();		
		}
		
		public function init(theColor:uint):void {			
			color=theColor;		
		}
		
		public function update():void  
		{
			lastPos = currentPos.clone();
			
			gravity = currentPos.clone();
			gravity.normalize(-0.05);
			
			acceleration = gravity;
			
			velocity = velocity.add(acceleration);
			
			//fuck motion
			velocity = velocity.add(new Point(-0.04 + Math.random()*0.08,-0.04 + Math.random()*0.08));
			//velocity = velocity.add(new Point(-3 + Math.random()*6,-3 + Math.random()*6));
			//currentPos = currentPos.add(new Point(-1 + Math.random()*2, -1 + Math.random()*2));
			
			currentPos = currentPos.add(velocity);
			
			lineThickness = velocity.length / 3;
		}
		
		public function draw():void  
		{
			graphics.clear();
			graphics.lineStyle(lineThickness,color,1);
			graphics.moveTo(lastPos.x, lastPos.y);
			graphics.lineTo(currentPos.x, currentPos.y);
			
			/*
			if(Math.random() > 0.95 && velocity.length < 2) {
				var size:Number = 2 + Math.random() * 20;
				graphics.beginFill(color,1);
				graphics.drawEllipse(currentPos.x + -5 + Math.random()*10, currentPos.y + -5 + Math.random()*10,size + Math.random() * 3,size + Math.random() * 3);
				graphics.endFill();			
			}
			*/
		}
	}
}